package com.Tools.WriteBoard
{
	import com.Events.WriteBoardEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	
	import mx.controls.ColorPicker;
	import mx.core.UIComponent;
	
	import spark.components.Group;
	
	public class WriteBoard extends Group
	{
		[Embed(source="com/Tools/Calligraphy/Brush/Brush.png")]
		private var brush_0:Class;
		
		public var tools:WBTools;//工具箱
		
		public var view_data:BitmapData;//显示容器数据
		
		public var view_bitmap:Bitmap;//显示容器
		
		public var uicomponent:UIComponent;//转换用容器
		
		public var content:Sprite;//绘制暂存   用于存放笔刷、临时生成的图案
		
		public var board:Sprite;//黑板  用于侦听鼠标动作
		
		///////////////////////////////////////////////////////////////////
		
		//burh
		private var brush_pen:Sprite;//移动的毛笔笔刷
		
		private var last_point:Point;//存储上次的位置
		
		private var oldScale:Number;//存储上次的缩放大小
		
		private var defaultScale:Number = 0.8;//默认毛笔笔触的大小
		
		private var bf:Array;//模糊滤镜
		
		//size color 
		private var pen_size:int;
		
		private var pen_color:Number;
		
		//draw mode
		private var draw_mode:String;
		//////////////////////////////////////////////////////////////////
		
		/**
		 * 画板，带有工具条。默认4种工具
		 * 1.pen 2.pencil  3.brush  4.eraser
		 * 
		 * 通过lock方法可以锁定（解锁）画板，让置于画板下面的元素得到鼠标响应
		 */
		public function WriteBoard()
		{
			super();
		}
		
		/**
		 * 初始化模块
		 */
		public function init_setting(_width:Number,_height:Number):void{
			this.width=_width;
			this.height=_height;
			
			//间接容器  隔离group与
			uicomponent=new UIComponent();
			this.addElement(uicomponent);
			
			//设置显示器
			view_data = new BitmapData(_width, _height,true,0x00ffffff);
			view_bitmap = new Bitmap(view_data);
			view_bitmap.smoothing = true;
			uicomponent.addChild(view_bitmap);
			
			//设置绘图板
			content = new Sprite();
			uicomponent.addChild(content);
			
			//设置黑板
			board=new Sprite();
			board.graphics.beginFill(0x000000,0.01);
			board.graphics.drawRect(0,0,_width,_height);
			board.graphics.endFill();
			uicomponent.addChild(board);
			
			//设置毛笔笔刷
			var temp_brush:Bitmap=new brush_0();
			brush_pen = new Sprite();
			brush_pen.addChild(temp_brush);
			brush_pen.alpha = 0.3;
			brush_pen.scaleX = brush_pen.scaleY = defaultScale;
			
		    bf=new Array(new BlurFilter(2,2));//滤镜
			brush_pen.filters = bf;
			
			//初始化工具箱
			tools=new WBTools();
			//this.addElement(tools);
			tools.addEventListener(MouseEvent.MOUSE_DOWN,onToolsMD);
			tools.addEventListener(WriteBoardEvent.PEN_SELECT,onpenselect);
			tools.addEventListener(WriteBoardEvent.SETTING_SELECT,onsizcolorselect);
			
			//初始化各种参数
			pen_size=1;
			pen_color=0x000000;
			draw_mode="pen";
			
			//黑板监听
			board.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
		}
		
		
		//笔修改
		private function onpenselect(event:WriteBoardEvent):void{
			if(event.pen=="pen"){
				draw_mode="pen";
			}else if(event.pen=="brush"){
				draw_mode="brush";
			}else if(event.pen=="pencil"){
				draw_mode="pencil";
			}else{//eraser
				draw_mode="eraser";
			}
			pen_size=event.size;
			pen_color=event.color;
		}
		
		//大小颜色修改  锁定 解锁  清空
		private function onsizcolorselect(event:WriteBoardEvent):void{
			//null是默认值  代表更改了颜色或尺寸
			if(event.setting=="null"){
				pen_size=event.size;
				pen_color=event.color;
			}else if(event.setting=="lock"){
				this.enabled=false;
			}else if(event.setting=="unlock"){
				this.enabled=true;
			}else{//清除
				view_data.dispose();
				view_data=new BitmapData(this.width, this.height,true,0x00ffffff);
				view_bitmap.bitmapData=view_data;
			}
		}
		
		//拖动
		private function onToolsMD(evnet:MouseEvent):void{
			tools.addEventListener(MouseEvent.MOUSE_UP,onToolsMU);
			tools.startDrag();
		}
		
		//停止拖动
		private function onToolsMU(event:MouseEvent):void{
			tools.removeEventListener(MouseEvent.MOUSE_UP,onToolsMU);
			tools.stopDrag();
		}
		
		//黑板上落下鼠标
		private function _mouseDown(e:MouseEvent):void
		{
			//设置笔位置与毛笔缩放参数
			last_point=new Point(mouseX,mouseY);
			oldScale = 1;
			
			//判断笔模式
			if(draw_mode=="brush"){
				//如果是毛笔则设置毛笔笔触大小
				if(pen_size<=5){
					defaultScale=0.5
				}else if(pen_size==10){
					defaultScale=0.8;
				}else if(pen_size==20){
					defaultScale=1;
				}else{
					defaultScale=1.5;
				}
				board.addEventListener(MouseEvent.MOUSE_MOVE, brush_draw);
			}else{
				if(draw_mode=="pen"){
					//钢笔则大小固定为1
					content.graphics.lineStyle(1,pen_color);
				}else if(draw_mode=="pencil"){
					//铅笔则使用大小  颜色设置
					content.graphics.lineStyle(pen_size,pen_color);
				}else{
					//橡皮则颜色固定为1
					content.graphics.lineStyle(pen_size,0xffffff);
				}
				content.graphics.moveTo(last_point.x,last_point.y);
				board.addEventListener(MouseEvent.MOUSE_MOVE, content_draw);
			}
			
			//监听鼠抬起
			board.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
		}
		
		//松开鼠标
		private function _mouseUp(e:MouseEvent):void
		{
			//移除监听  移除笔  停止拖动
			board.removeEventListener(MouseEvent.MOUSE_MOVE, content_draw);
			board.removeEventListener(MouseEvent.MOUSE_MOVE,brush_draw);
			board.removeEventListener(MouseEvent.MOUSE_UP, _mouseUp);
			
			brush_pen.stopDrag();
			
			if(draw_mode=="brush"){
				view_data.draw(content);
				while(content.numChildren>0){
					content.removeChildAt(0);
				}
			}else{
				view_data.draw(content,null,null,draw_mode=="eraser" ? BlendMode.ERASE:BlendMode.NORMAL)
				//清除内容
				content.graphics.clear();
			}
		}
		
		//绘制图形
		private function content_draw(event:MouseEvent):void{
			content.graphics.lineTo(event.localX,event.localY);
		}
		
		//绘制毛笔
		private function brush_draw(event:MouseEvent):void{
			var new_point:Point=new Point(mouseX,mouseY);
			
			var dis:Number=Point.distance(new_point,last_point);
			
			//改变笔触的大小,越快越小
			if (dis > 0) {
				var scale:Number = defaultScale - dis * 0.02;//控制粗细变化
				if (scale > 1) {
					scale = 1;
				}else if (scale < 0.08) {
					scale = 0.08;
				}
				scale = (oldScale + scale) / 2;
			}
			
			//填充
			var count:int = Math.floor(dis);
			var scaleBili:Number = (oldScale-scale) / count;
			for (var i:int=0; i<count-1; i++) {
				var brush1:Bitmap = new brush_0();
				brush1.filters = bf;
				brush1.alpha = 0.3;
				brush1.scaleX = brush1.scaleY = oldScale-i * scaleBili;
				brush1.x=((new_point.x-last_point.x)/count)*(i+1)+last_point.x;
				brush1.y=((new_point.y-last_point.y)/count)*(i+1)+last_point.y;
				content.addChild(brush1);
			}
			oldScale = scale;
			last_point=new_point;
		}
	}
}